rm(list = ls())

# install.packages("ggplot2")
# install.packages("pillar")
# install.packages("magrittr")
# install.packages("ggrepel")
# install.packages("tidyverse")
# install.packages("plyr")
# call necessary packages

library(ggplot2)
library(ggrepel)
library(tidyverse)
library(plyr)

# load data 
mal <- read_csv("mal_jitter.csv")

mal$fraud_category <- as.character(mal$both_fraud)
mal$fraud_category <- revalue(mal$fraud_category, c("0" = "No Violence and Cheating", "1" = "Only Violence", "2" = "Only Cheating", "3"= "Both Violence and Cheating"))
mal$fraud_category  = factor(mal$fraud_category, levels=c("No Violence and Cheating", "Only Violence", "Only Cheating", "Both Violence and Cheating"))
mal$oecd_category <- as.character(mal$oecd)

# Figure 1: Jitter-violin plot
mal_plot <- mal %>%
    filter(fraud_category!="NA", sample==1, fraud_category!="No Violence and Cheating") %>% 
    ggplot(aes(y = fraud_category, x = mal1)) +
    geom_violin(alpha = 0.3) +
    geom_jitter(alpha = 0.7) +
    stat_summary(mal1=mean, geom="point", size=4, color="red") +
    ggtitle("Figure 1: Jitter-Violin Plot") +
    labs( y = "Blatant Electoral Fraud", x = "Levels of Malapportionment")
 
 mal_plot
 
 

